package yagwl.service.fnd.flex;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import java.util.List;
import java.util.logging.Logger;

import yagwl.service.util.svc.CacheService;
import yagwl.service.util.svc.CacheService.CacheLoaderCallback;
import yagwl.service.util.svc.Context;
import yagwl.service.util.svc.ServiceInvocationHandler;

public class FlexEntityServiceImpl implements FlexEntityService {

  private static final Logger log = Logger.getLogger(FlexEntityServiceImpl.class
      .getName());
  private static CacheService cacheSvc;
  private static FlexEntityCacheLoaderCallback flexEntityCallback;
  private static FlexEntityServiceImpl svc = new FlexEntityServiceImpl();

  private FlexEntityServiceImpl() {
    if (cacheSvc != null)
      return;
    cacheSvc = CacheService.getInstance();
    flexEntityCallback = this.new FlexEntityCacheLoaderCallback();
    cacheSvc.registerCacheLoaderCallback(
        FlexEntityEntity.class.getSimpleName(), flexEntityCallback);
  }

  public static FlexEntityService getInstance() {
    InvocationHandler handler = new ServiceInvocationHandler(svc);
    FlexEntityService proxy = (FlexEntityService) Proxy.newProxyInstance(svc
        .getClass().getClassLoader(), svc.getClass().getInterfaces(), handler);
    return proxy;
  }

  private class FlexEntityCacheLoaderCallback implements CacheLoaderCallback {
    @Override
    public List<?> loadList(List<Long> objectIds) {
      return null;
    }

    @Override
    public Object load(Long objectId) {
      Context ctx = Context.getCurrentInstance();
      FlexEntityDao flexDao = new FlexEntityDao(ctx.getDaoContext());
      //FlexEntityEntity cat = extDao.getCategoryTree();
      return null;
    }
  }

  @Override
  public FlexEntityEntity createFlexEntity(FlexEntityEntity flexEntity) {
    Context ctx = Context.getCurrentInstance();
    FlexEntityDao flexDao = new FlexEntityDao(ctx.getDaoContext());
    FlexEntityEntity ent = flexDao.createExtEntity(flexEntity);
    cacheSvc.invalidate(FlexEntityEntity.class.getSimpleName(), 0L);
    return ent;
  }

  @Override
  public List<FlexEntityEntity> getFlexEntities() {
    Context ctx = Context.getCurrentInstance();
    FlexEntityDao flexDao = new FlexEntityDao(ctx.getDaoContext());
    List<FlexEntityEntity> list = flexDao.getFlexEntities();
    return list;
  }

}
